package src.TestSuite;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import src.MyDynamicStack;

public class MyDynamicStackTest {	
	private int size = 5;
	private int entry = 1;
	private MyDynamicStack MyDS = new MyDynamicStack(size);
	private final ByteArrayOutputStream outConsole = new ByteArrayOutputStream();
	
	@Before
	public void setOutStream() {
		System.setOut(new PrintStream(outConsole));
	}
	
	@After
	public void cleanOutStream() {
		System.setOut(null);
	}

	@Test
	public void testMyDynamicStack() {
		assertNotNull(MyDS);
	}
	
	public void testPush() {
		for(int i=0; i<size; i++) {
			MyDS.push(entry++);
		}
		assertTrue(MyDS.isStackFull());
		MyDS.push(entry++);
		assertEquals("Stack is full. Increasing the capacity.\r\n", outConsole.toString());
		assertFalse(MyDS.isStackFull());
		testPeek();		
	}

	@Test
	public void testPushPop() {
		testPush();
		try {
			while(!MyDS.isStackEmpty()) {
				MyDS.pop();
			}
			MyDS.pop();
		} catch(Exception ex) {
			assertEquals("Stack is empty. Can not remove element.",ex.getMessage());
		}
	}

	public void testPeek() {
		long peek = MyDS.peek();
		assertEquals(6,peek);
	}
}
